define(function(require){
    var main =require('/static/main.js');
    require('/static/ajaxfileupload.js');
    var m = new main();
    function Cont(){
        this.Fun._init();
    }
    Cont.prototype.Fun = {
        _init:function(){
            this.getlist();
            this.bindEvent();
        },
        bindEvent:function(){
        },
        getlist:function(){
            var _this = this;
            var emdataa = [];
            var param = {};
            var sort = 0;
            var table = $('#datatable').dataTable({
                'ordering':false,
                "serverSide": true,
                "searching":false,
                'bLengthChange': false,
                'bPaginate': true,
                "bDestory": true,
                "bRetrieve": true,
                "bFilter":false,
                "bSort": false,
                "bProcessing": true,
                "autoWidth": false,
                ajax: function (data, callback, settings) {
                    //封装请求参数
                    param.PageSize = data.length;//页面显示记录条数，在页面显示每页显示多少项的时候
                    param.start = data.start;//开始的记录序号
                    param.PageNo = (data.start / data.length)+1;//当前页码
                    param.ENTCode = _this.ENTCode;//当前页码


                    sort = (param.PageNo-1)*param.PageSize;
                    //ajax请求数据
                    $.ajax({
                        type: "POST",
                        url: "/website/get-case-list",
                        cache: false,  //禁用缓存
                        data: param,  //传入组装的参数
                        dataType: "json",
                        success: function (result) {
                            var returnData = {};
                            returnData.draw = data.draw;
                            returnData.recordsTotal = result.total;
                            returnData.recordsFiltered = result.total;
                            returnData.data = result.data;
                            emdataa = result.data;
                            callback(returnData);
                        }
                    });
                },
                "aoColumns":[
                    {"mDataProp": 'Src',
                        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                            $(nTd).html('<img style="with:60px;height:40px;" src="'+oData.ImgHost +oData.Src +'">');

                        }
                    },
                    {"mDataProp": 'Title'},
                    {"mDataProp": 'CompanyTypeName'},
                    {"mDataProp": 'CompanyName'},
                    {"mDataProp": 'FansCnt'},
                    {"mDataProp": 'CreateTime',
                        render:function(data){
                            return m.formatTimeAll2(data);
                        }
                    },
                    {
                        "mDataProp": "CaseId",
                        "sClass": "option_btn",
                        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                            // console.log(arguments);
                            $(nTd).html('<span class="btn_detial LookBtn" data-id="'+sData+'">查看</span>\
                                            <span class="btn_detial EditBtn" data-id="'+sData+'">编辑</span>\
                                            <span class="btn_del DelBtn"  data-id="'+sData+'">删除</span>');

                        }
                    }
                ]
            }).api();

            // 点击搜索
            $('#btnSearch').click(function(){
                param.ComName = $('.SearchComName').val();
                var table = $('#datatable').DataTable();
                table.ajax.reload();

            });
            // 点击重置按钮
            $('#btnRefresh').click(function(){
                $('.SearchComName').val('');
                param.ComName = '';
                var table = $('#datatable').DataTable();
                table.ajax.reload();
            });

            //查看
            $(document).on('click','.LookBtn',function(){
                var CaseId = $(this).attr("data-id");
                $.each(emdataa,function(k,v){
                    if(CaseId == v.CaseId){
                        var str ='<dt class="">主图：</dt><dd class=""><img src="'+ (v.ImgHost+v.Src)+'"  style="width:50%;height:100px;"> </dd>\
                            <dt class="m-t-10">标题：</dt><dd class="m-t-10">'+ v.Title+'</dd>\
                            <dt class="m-t-10">公司名称：</dt><dd class="m-t-10">'+ v.CompanyName+'</dd>\
                            <dt class="m-t-10">企业类型：</dt><dd class="m-t-10">'+ v.CompanyTypeName+'</dd>\
                            <dt class="m-t-10">描述：</dt><dd class="m-t-10">'+ v.Description +' </dd>';

                        $("#Detail .dl-horizontal").html(str);
                        $("#Detail").modal('show');
                        return false;
                    }
                });
            });
            //编辑
            $(document).on('click','.EditBtn',function(){
               var caseid = $(this).attr("data-id");
                window.location.href = '/website/addcase?caseid='+caseid;
            });
            //删除
            $(document).on('click','.DelBtn',function(){
                var caseid = $(this).attr("data-id");
                $.ajax({
                    type: "POST",
                    url: "/website/case-delete",
                    data :{CaseID:caseid},
                    dataType: "json",
                    success: function (ret){
                        isactive = false;
                        if(ret.IsBizSuccess){
                            layer.msg("删除成功！");
                            var table1 = $('#datatable').DataTable();
                            table1.ajax.reload();
                        }else{
                            layer.msg(ret.ErrMsg);
                        }
                    }
                });
            });
        }

    }
    var a = new Cont();

})